home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9014 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  67 lines

  1. Path: nntphub.cb.att.com!not-for-mail
  2. From: ka@socrates.hr.att.com (Kenneth Almquist)
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: on OO differnces between Ada95 and C++
  5. Date: 28 Feb 1996 00:45:29 GMT
  6. Organization: AT&T Bell Laboratories, Columbus, Ohio
  7. Message-ID: <4h08j9$f7v@nntpa.cb.att.com>
  8. References: <4gbq7q$g08@qualcomm.com> <3129F185.41C6@Rational.COM> <4gi413$qo1@druid.borland.com> <312D8EF7.167E@Rational.COM>
  9. NNTP-Posting-Host: socrates.hr.att.com
  10.  
  11. Jerome Desquilbet <jDesquilbet@Rational.COM> asks:
  12. > Pete,
  13. > What is this "one definition rule"? Could you point me to the
  14. > appropriate C++ Draft Standard location?
  15.  
  16. I'm not Pete, but I happen to know the answer.  See section 3.2 in the
  17. April draft.  The relevant paragraph reads:
  18.  
  19.   7 There  can  be  more than one definition of a class type in a program
  20.     provided that each definition appears in a different translation unit
  21.     and the definitions describe the same type.
  22.  
  23. > It seems that in C++, every encapsulation can be legally broken,
  24. > essentially because of _independent_ compilation that allows
  25. > redefinitions of the same class.
  26.  
  27. No, your example is not legal.  The draft continues:
  28.  
  29.   8 No diagnostic is required for a violation of the ODR rule.
  30.  
  31. Using the language of the Ada RM, a program which violates the ODR
  32. is erroneous.  The proposed standard says NOTHING about the run time
  33. behavior of your example.
  34.  
  35. To see some of the other implications of this, suppose that I'm
  36. developing some code and need a rectangle class.  I write:
  37.  
  38.     class Rectange {
  39.         Point lower_left_corner;
  40.         int height;
  41.         int width;
  42.     };
  43.  
  44. I unit test my code and it works fine.  Now suppose that while I'm
  45. doing this, one of my co-workers is also developing code for the same
  46. project, and defines the type:
  47.  
  48.     class Rectange {
  49.         Point lower_left_corner;
  50.         Point upper_right_corner;
  51.     };
  52.  
  53. Like me, he unit tests his code and it works.  But when we build the
  54. entire program, the program violates the ODR because it contains two
  55. different defintions of Rectangle.  The standard permits the program
  56. to fail in surprising and interesting ways.  This makes it difficult
  57. to develope large programs in standard C++!
  58.  
  59. The emphasis here is of course on "standard."  The programmers' intents
  60. were that the two Rectangle classes be distinct types, and that the
  61. compiler ignore the fact that they happen to have the same name.  An
  62. implementation may do precisely that, and perhaps all implementations
  63. will.  The designers of Ada were concerned enough about programming
  64. in the large to address this sort of issue in the standard itself
  65. rather than leaving it as a "quality of implementation" issue.
  66.                 Kenneth Almquist
  67.